//
//  WSJSportCircleViewController.m
//  RunningMan
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSJSportCircleViewController.h"
#import "WSSportCircleCell.h"
#import "WSDietViewController.h"
#import "WSAskViewController.h"
#import "WSMotivationalViewController.h" //励志
#import "WSForumViewController.h"
#import "WSMyTool.h"
@interface WSJSportCircleViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSArray *titleArray;
@end

@implementation WSJSportCircleViewController

- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSArray array];
    }
    return _titleArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollectionView];
    [self createChildViewController];
    [self createScrollView];
    
}
- (void)createCollectionView {
    self.titleArray = @[@"饮食", @"问答", @"励志", @"论坛"];
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(50 * FITWIDTH, 40 * FITWIDTH);
    layout.minimumLineSpacing = 40 * FITWIDTH;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 80 * self.titleArray.count * FITWIDTH, 40 * FITWIDTH) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[WSSportCircleCell class] forCellWithReuseIdentifier:NSStringFromClass([WSSportCircleCell class])];

    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView = self.collectionView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSSportCircleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WSSportCircleCell class]) forIndexPath:indexPath];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    
    if (indexPath.row == self.index) {
        cell.titleLabel.font = [UIFont boldSystemFontOfSize:20 * FITWIDTH];
        cell.titleLabel.textColor = [UIColor blueColor];
    } else {
        cell.titleLabel.font = [UIFont systemFontOfSize:17 * FITWIDTH];
        cell.titleLabel.textColor = [UIColor darkGrayColor];
    }
    return cell;
}
- (void)createChildViewController {
    WSDietViewController *diet = [WSDietViewController new];
    [self addChildViewController:diet];
    
    WSAskViewController *ask = [WSAskViewController new];
    [self addChildViewController:ask];
    
    WSMotivationalViewController *motivational = [WSMotivationalViewController new];
    [self addChildViewController:motivational];
    
    WSForumViewController *forum = [WSForumViewController new];
    [self addChildViewController:forum];
    
}
- (void)createScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(WIDTH * self.titleArray.count, 0);
    [self.view addSubview:self.scrollView];
    
    for (int i = 0; i < self.titleArray.count; i++) {
        UIViewController *controller = self.childViewControllers[i];
        controller.view.frame = CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT);
        [self.scrollView addSubview:controller.view];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.index = indexPath.row;
    self.scrollView.contentOffset = CGPointMake(self.index * WIDTH, 0);
}
// scrollView滚动时就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.index = self.scrollView.contentOffset.x / WIDTH;
    // 滑动scrollView的时候collectionView就滚动到相应位置
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
    [self.collectionView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
